package com.gwcloud.leetcode.hot100;

import java.util.HashMap;
import java.util.Map;

/** 
* @ClassName   : Leetcode0003 
* @author      : ganwei 
* @date        : 2020年5月19日 
* @Description : 无重复字符的最长子串修改
*/
public class Leetcode0003 {
    public int lengthOfLongestSubstring(String s) {
    	int left = 0, right = 0;
    	Map<Character, Integer> window = new HashMap<Character, Integer>();
    	int res = 0;
    	
    	while(right < s.length()) {
    		char c = s.charAt(right);
    		int count = window.getOrDefault(c, 0);
    		window.put(c, count + 1);
    		
    		while(window.get(c) > 1 && left <= right) {
    			char c1 = s.charAt(left);
    			int count1 = window.getOrDefault(c1, 0);
    			if(count1 != 0) {
    				window.put(c1, window.get(c1) - 1);
    			}
    			left++;
    		}
    		res = Math.max(res, right-left+1);
    		right++;
    	}
    	
    	return res;
    }
    
    public static void main(String[] args) {
    	String s = "abcabcbb";
    	Leetcode0003 l0003 = new Leetcode0003();
    	System.out.println(l0003.lengthOfLongestSubstring(s));
    }
}
